

//单片机头文件
#include "stm32f10x.h"

//网络协议层
#include "onenet.h"

//网络设备
#include "esp8266.h"

//硬件驱动
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "dht11.h"
#include "oled.h"
#include "buzzer.h"
#include "lightsensor.h"
#include "adc.h"
#include "tim2.h"

//C库
#include <string.h>

#define ESP8266_ONENET_INFO		"AT+CIPSTART=\"TCP\",\"mqtts.heclouds.com\",1883\r\n"

void Hardware_Init(void);
void Display_Init(void);
void Refresh_Data(void);

u8 temp,humi,light,gas;
u16 data[2] = {0};

/*
************************************************************
*	函数名称：	main
*
*	函数功能：	
*
*	入口参数：	无
*
*	返回参数：	0
*
*	说明：		
************************************************************
*/
int main(void)
{
	
	unsigned short timeCount = 0;	//发送间隔变量
			
			unsigned char *dataPtr = NULL;
	
	Hardware_Init();				//初始化外围硬件
	
			ESP8266_Init();					//初始化ESP8266


			UsartPrintf(USART_DEBUG, "Connect MQTTs Server...\r\n");
			while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT"))
				DelayXms(500);
			UsartPrintf(USART_DEBUG, "Connect MQTT Server Success\r\n");
			
			while(OneNet_DevLink())			//接入OneNET
				DelayXms(500);

			OneNET_Subscribe();
	
	while(1) 
	{
		gas = data[1] * 100 / 4095;
		if(++timeCount >= 100)									//发送间隔5s
		 {
			DHT11_Read_Data(&temp,&humi);
			light = data[0] * 100 / 4095;
			light = 0x0064 - light;
			//UsartPrintf(USART_DEBUG, "ADC1_ReadV = %d\r\n", data[0] * 3.3 / 4095);
			
			//UsartPrintf(USART_DEBUG,"V变阻器 = %.2f, V_PC2 = %.2f\n", data[0] * 100.0 / 4095, data[1] * 3.3 / 4095);
			//UsartPrintf(USART_DEBUG, "V变阻器 = %2d, V_PC2 = %2d\n", data[0] * 100 / 4095, data[1] * 100 / 4095);
				//	UsartPrintf(USART_DEBUG, "OneNet_SendData\r\n");
					OneNet_SendData();									//发送数据
			
			timeCount = 0;
					ESP8266_Clear();
		}
		// if(LightSensor_Get())Led_Set(LED_ON);				//暗度，亮灯
		// else Led_Set(LED_OFF);								//亮度，灭灯
		//myCode_start
		
		DelayMs(10);
		TIM2_SetBrightness(light);
		//烟雾检测
		if(gas > 90){
			Buzzer_ON();
			DelayMs(100);
			Buzzer_OFF();
		}

		//myCode_end

				
				dataPtr = ESP8266_GetIPD(0);
				if(dataPtr != NULL)
					OneNet_RevPro(dataPtr);
		
		Refresh_Data();
		
		//DelayMs(40);
	
	}

}
/*
************************************************************
*	函数名称：	Hardware_Init
*
*	函数功能：	硬件初始化
*
*	入口参数：	无
*
*	返回参数：	无
*
*	说明：		初始化单片机功能以及外接设备
************************************************************
*/
void Hardware_Init(void)
{
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	//中断控制器分组设置

	Delay_Init();									//systick初始化
	
	Usart1_Init(115200);							//串口1，打印信息用
	
	Usart2_Init(115200);							//串口2，驱动ESP8266用
	
  	Key_Init();
	
	Led_Init();									//蜂鸣器初始化
	
	LightSensor_Init();							//光敏传感器初始化

	Buzzer_Init();									//蜂鸣器初始化

	ADC1_Init();									//ADC初始化

	//ADC1_StartConvert();
	ADC1_DMA_Init();

	ADC1_DMA_StartConvert((uint32_t)data, 2);

	TIM2_Init();									//TIM2初始化
	TIM2_Start();									//TIM2开始工作
	
	while(DHT11_Init())
	{
		UsartPrintf(USART_DEBUG, "DHT11 Error \r\n");
		DelayMs(1000);
	}
	
	Display_Init();
	
	UsartPrintf(USART_DEBUG, " Hardware init OK\r\n");
	
}

void Display_Init(void)
{
	OLED_Init();			//初始化OLED  
	OLED_Clear(); 
	
	OLED_ShowCHinese(0,0,1);//温
	OLED_ShowCHinese(18,0,4);//湿
	OLED_ShowCHinese(36,0,2);//度
	OLED_ShowCHinese(54,0,0);//：
	OLED_ShowCHinese(78,0,3);//℃
	OLED_ShowString(114,0,"%",16);//%

	
	OLED_ShowCHinese(0,3,12);//湿
	OLED_ShowCHinese(18,3,13);//度
	OLED_ShowCHinese(36,3,0);//：
	OLED_ShowString(72,3,"%",16);//%


	OLED_ShowString(72,6,"%",16);//%
	
	OLED_ShowCHinese(0,6,10);//台
	OLED_ShowCHinese(18,6,11);//灯
	OLED_ShowCHinese(36,6,0);//：
	
}
void Refresh_Data(void)
{
	char buf[10];
	sprintf(buf, "%2d", temp);
	OLED_ShowString(60,0,buf,16); //温度值
	
	sprintf(buf, "%2d", humi);
	OLED_ShowString(96,0,buf,16); //湿度值

	sprintf(buf, "%2d", light);
	OLED_ShowString(54,3,buf,16); //光照值
	
	sprintf(buf, "%2d", gas);
	OLED_ShowString(54,6,buf,16); //光照值
	// if(led_info.Led_Status) OLED_ShowCHinese(54,6,8);//亮
	// else OLED_ShowCHinese(54,6,9);//灭
	
}
